/*
 * MembershipFrame.java
 *
 * Created on February 18, 2007, 11:25 PM
 */

package edu.byu.isys413.group1E.gui;

import edu.byu.isys413.group1E.*;
import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author  tkulbeth
 */
public class MembershipFrame extends javax.swing.JFrame {
    
    MembershipBO member = null;
    ArrayList<String> currentAvail = null;
    CustomerFrame callingFrame = null;
    private String CCNum = null;
    
    /** Creates new form MembershipFrame */
    public MembershipFrame() {
        initComponents();
    }
    
    /** Creates a new form MembershipFrame, along with the member object being created with the referring customer BO*/
    public MembershipFrame(CustomerBO cust, CustomerFrame custF) {
        initComponents();
        buildFrame(cust);
        callingFrame = custF;
    }
    
    /*builds the MembershipFrame according to incoming data*/
    public void buildFrame(CustomerBO cust) {
        if(cust.getCustMembership() == null){
            try {
                member = MembershipDAO.getInstance().create();
                member.setCust(cust);
                //check if this is a brand-new membership
                if(member.isAlreadyInDB() == false){
                    Calendar cal = Calendar.getInstance();
                    Date today = cal.getTime();
                    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
                    String startDate = fmt.format(today);
                    member.setMemberStartDate(startDate);
                    
                    //set member expiration date for 2 years ahead by default
                    cal.add(Calendar.YEAR,2);
                    today = cal.getTime();
                    String expDate = fmt.format(today);
                    member.setMemberExpireDate(expDate);
                }
                cust.setCustMembership(member);
                this.field_memberStartDate.setText(member.getMemberStartDate());
                this.field_memberExpireDate.setText(member.getMemberExpireDate());
                this.field_backupExpDate.setText("");
                this.field_backupExpDate.setEnabled(false);
                this.field_backupSize.setText("");
                this.field_backupSize.setEnabled(false);
                this.radio_backupNo.setSelected(true);
                this.radio_backupYes.setSelected(false);
                
                //build the updated AOI selection list
                this.updateSelectedListView();
                this.updateListView();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }else{
            member = cust.getCustMembership();
            member.setCust(cust);
            this.cboBox_CreditCardType.setSelectedItem(member.getCreditCardType());
            this.cboBox_CreditCardType.updateUI();
            this.field_creditCardNum.setText(member.getCreditCardNum());
            CCNum = member.getCreditCardNum();
            this.field_creditCardNum.setText("****************");
            this.cboBox_Month.setSelectedItem(Integer.toString(member.getCreditCardExpMonth()));
            this.cboBox_Month.updateUI();
            this.cboBox_Year.setSelectedItem(Integer.toString(member.getCreditCardExpYear()));
            this.cboBox_Year.updateUI();
            this.field_memberStartDate.setText(member.getMemberStartDate());
            this.field_memberExpireDate.setText(member.getMemberExpireDate());
            if(member.getBuExpDate() != null){
                this.field_backupExpDate.setText(member.getBuExpDate());
                this.field_backupExpDate.setEnabled(true);
                this.field_backupSize.setText(Double.toString(member.getBuSize()));
                this.field_backupSize.setEnabled(true);
                this.radio_backupYes.setSelected(true);
                this.radio_backupNo.setSelected(false);
            }else{
                this.field_backupExpDate.setText("");
                this.field_backupExpDate.setEnabled(false);
                this.field_backupSize.setText("");
                this.field_backupSize.setEnabled(false);
                this.radio_backupNo.setSelected(true);
                this.radio_backupYes.setSelected(false);
            }
            this.updateSelectedListView();
            this.updateListView();
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        field_memberStartDate1 = new javax.swing.JTextField();
        label_MembershipHead = new javax.swing.JLabel();
        field_creditCardNum = new javax.swing.JTextField();
        label_creditCardNum = new javax.swing.JLabel();
        label_creditCardExpDate = new javax.swing.JLabel();
        cboBox_Month = new javax.swing.JComboBox();
        label_creditCardMonth = new javax.swing.JLabel();
        label_creditCardYear = new javax.swing.JLabel();
        cboBox_Year = new javax.swing.JComboBox();
        label_creditCardType = new javax.swing.JLabel();
        cboBox_CreditCardType = new javax.swing.JComboBox();
        label_memberStartDate = new javax.swing.JLabel();
        label_memberExpireDate = new javax.swing.JLabel();
        field_memberStartDate = new javax.swing.JTextField();
        field_memberExpireDate = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_avail_AOI = new javax.swing.JList();
        label_AOISelection = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        list_selectedAOI = new javax.swing.JList();
        button_selectAOI = new javax.swing.JButton();
        button_deselectAOI = new javax.swing.JButton();
        label_availAOI = new javax.swing.JLabel();
        label_selectedAOI = new javax.swing.JLabel();
        button_addNewAOI = new javax.swing.JButton();
        button_cancelMembershipChanges = new javax.swing.JButton();
        button_SaveMembership = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        label_AOISelection1 = new javax.swing.JLabel();
        radio_backupYes = new javax.swing.JRadioButton();
        radio_backupNo = new javax.swing.JRadioButton();
        field_backupExpDate = new javax.swing.JTextField();
        label_backupExpDate = new javax.swing.JLabel();
        field_backupSize = new javax.swing.JTextField();
        label_backupSize = new javax.swing.JLabel();
        button_checkBuPrice = new javax.swing.JButton();

        field_memberStartDate1.setText("<yyyy-mm-dd>");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Membership Information");
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 800, 800));
        label_MembershipHead.setText("Enter Membership Information:");

        field_creditCardNum.setText("<Credit Card #>");
        field_creditCardNum.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                field_creditCardNumFocusGained(evt);
            }
        });

        label_creditCardNum.setFont(new java.awt.Font("Dialog", 1, 10));
        label_creditCardNum.setText("Credit Card #: *");

        label_creditCardExpDate.setFont(new java.awt.Font("Dialog", 1, 10));
        label_creditCardExpDate.setText("Expiration Date: *");

        cboBox_Month.setFont(new java.awt.Font("Dialog", 0, 10));
        cboBox_Month.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<select>", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        label_creditCardMonth.setFont(new java.awt.Font("Dialog", 1, 10));
        label_creditCardMonth.setText("Month:");

        label_creditCardYear.setFont(new java.awt.Font("Dialog", 1, 10));
        label_creditCardYear.setText("Year:");

        cboBox_Year.setFont(new java.awt.Font("Dialog", 0, 10));
        cboBox_Year.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<select>", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025" }));

        label_creditCardType.setFont(new java.awt.Font("Dialog", 1, 10));
        label_creditCardType.setText("Credit Card Type: *");

        cboBox_CreditCardType.setFont(new java.awt.Font("Dialog", 0, 10));
        cboBox_CreditCardType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Select>", "Visa", "Mastercard", "Discover", "AmericanExpress" }));

        label_memberStartDate.setFont(new java.awt.Font("Dialog", 1, 10));
        label_memberStartDate.setText("Membership Start Date (YYYY-MM-DD): *");

        label_memberExpireDate.setFont(new java.awt.Font("Dialog", 1, 10));
        label_memberExpireDate.setText("Membership Expiration Date (YYYY-MM-DD): *");

        field_memberStartDate.setText("<yyyy-mm-dd>");

        field_memberExpireDate.setText("<yyyy-mm-dd>");

        list_avail_AOI.setFont(new java.awt.Font("Dialog", 1, 10));
        jScrollPane1.setViewportView(list_avail_AOI);

        label_AOISelection.setText("Select Areas of Interest:");

        list_selectedAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        jScrollPane2.setViewportView(list_selectedAOI);

        button_selectAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        button_selectAOI.setText("Add>>");
        button_selectAOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_selectAOIActionPerformed(evt);
            }
        });

        button_deselectAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        button_deselectAOI.setText("<<Remove");
        button_deselectAOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_deselectAOIActionPerformed(evt);
            }
        });

        label_availAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        label_availAOI.setText("Available:");

        label_selectedAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        label_selectedAOI.setText("Selected:");

        button_addNewAOI.setFont(new java.awt.Font("Dialog", 1, 10));
        button_addNewAOI.setText("Add New");
        button_addNewAOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_addNewAOIActionPerformed(evt);
            }
        });

        button_cancelMembershipChanges.setFont(new java.awt.Font("Dialog", 1, 10));
        button_cancelMembershipChanges.setText("Cancel");
        button_cancelMembershipChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_cancelMembershipChangesActionPerformed(evt);
            }
        });

        button_SaveMembership.setFont(new java.awt.Font("Dialog", 1, 10));
        button_SaveMembership.setText("Save");
        button_SaveMembership.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_SaveMembershipActionPerformed(evt);
            }
        });

        label_AOISelection1.setText("Backup Service:");

        radio_backupYes.setText("Yes");
        radio_backupYes.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        radio_backupYes.setMargin(new java.awt.Insets(0, 0, 0, 0));
        radio_backupYes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radio_backupYesItemStateChanged(evt);
            }
        });

        radio_backupNo.setText("No");
        radio_backupNo.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        radio_backupNo.setMargin(new java.awt.Insets(0, 0, 0, 0));
        radio_backupNo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radio_backupNoItemStateChanged(evt);
            }
        });

        field_backupExpDate.setEditable(false);
        field_backupExpDate.setText("<yyyy-mm-dd>");

        label_backupExpDate.setFont(new java.awt.Font("Dialog", 1, 10));
        label_backupExpDate.setText("Backup Expiration Date (YYYY-MM-DD): ");

        field_backupSize.setEditable(false);
        field_backupSize.setText("<Size>");

        label_backupSize.setFont(new java.awt.Font("Dialog", 1, 10));
        label_backupSize.setText("Backup Size (MB):");

        button_checkBuPrice.setFont(new java.awt.Font("Dialog", 1, 10));
        button_checkBuPrice.setText("Check Price");
        button_checkBuPrice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_checkBuPriceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_memberStartDate)
                        .addGap(45, 45, 45)
                        .addComponent(field_memberStartDate, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_memberExpireDate)
                        .addGap(17, 17, 17)
                        .addComponent(field_memberExpireDate, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                    .addComponent(label_AOISelection)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label_availAOI))
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(button_selectAOI)
                            .addComponent(button_deselectAOI))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_selectedAOI)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(button_addNewAOI)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                        .addComponent(button_SaveMembership)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_cancelMembershipChanges))
                    .addComponent(label_MembershipHead)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cboBox_CreditCardType, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label_creditCardNum, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(field_creditCardNum, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label_creditCardMonth)
                                .addGap(1, 1, 1)
                                .addComponent(cboBox_Month, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(label_creditCardYear)
                                .addGap(2, 2, 2)
                                .addComponent(cboBox_Year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(label_creditCardExpDate)))
                    .addComponent(label_creditCardType)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label_AOISelection1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radio_backupYes)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radio_backupNo))
                            .addComponent(label_backupExpDate, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(button_checkBuPrice)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label_backupSize, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(field_backupExpDate, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(field_backupSize, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {button_deselectAOI, button_selectAOI});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {button_SaveMembership, button_cancelMembershipChanges});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_MembershipHead)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_creditCardType)
                        .addGap(1, 1, 1)
                        .addComponent(cboBox_CreditCardType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label_creditCardNum)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(field_creditCardNum, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_creditCardExpDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_creditCardMonth)
                            .addComponent(label_creditCardYear)
                            .addComponent(cboBox_Year, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboBox_Month, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_memberStartDate)
                    .addComponent(field_memberStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_memberExpireDate)
                    .addComponent(field_memberExpireDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_AOISelection)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_availAOI)
                            .addComponent(label_selectedAOI))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(button_addNewAOI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button_cancelMembershipChanges, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button_SaveMembership, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(button_selectAOI)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_deselectAOI)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_AOISelection1)
                    .addComponent(radio_backupYes)
                    .addComponent(radio_backupNo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(field_backupExpDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_backupExpDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(field_backupSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_backupSize)
                    .addComponent(button_checkBuPrice))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void radio_backupYesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radio_backupYesItemStateChanged
        if(this.radio_backupYes.isSelected()){
            this.radio_backupNo.setSelected(false);
        }
    }//GEN-LAST:event_radio_backupYesItemStateChanged
    
    private void radio_backupNoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radio_backupNoItemStateChanged
        if(this.radio_backupNo.isSelected()){
            this.radio_backupYes.setSelected(false);
        }
    }//GEN-LAST:event_radio_backupNoItemStateChanged
    
    private void button_checkBuPriceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_checkBuPriceActionPerformed
        try {                                                                           //change appearance according to system
            String look = UIManager.getSystemLookAndFeelClassName();
            UIManager.setLookAndFeel(look);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        BackupPriceFrame bpf = new BackupPriceFrame();
        bpf.setLocationRelativeTo(null);
        bpf.setVisible(true);                                  //open the BackupPriceFrame
    }//GEN-LAST:event_button_checkBuPriceActionPerformed
    
    private void button_deselectAOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_deselectAOIActionPerformed
        //check if a selection has been made
        if(list_selectedAOI.getSelectedIndex() != -1){
            //if so, get the selected values from the available AOI list
            int[ ] selections = list_selectedAOI.getSelectedIndices();
            for(int i=(selections.length-1); i>=0; i--){
                try {
                    //remove to the selected list
                    MembershipDAO.getInstance().removeInterest(member, selections[i]);
                } catch (SQLException s){
                    s.printStackTrace();
                } catch (DataException ex) {
                    ex.printStackTrace();
                } catch (ConnectionPoolException ex) {
                    ex.printStackTrace();
                }
            }
            this.updateSelectedListView();
            this.updateListView();
        }
    }//GEN-LAST:event_button_deselectAOIActionPerformed
    
    private void button_selectAOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_selectAOIActionPerformed
        //check if a selection has been made
        if(list_avail_AOI.getSelectedIndex() != -1){
            //if so, get the selected values from the available AOI list
            int[ ] selections = list_avail_AOI.getSelectedIndices();
            for(int i=0; i<selections.length; i++){
                try {
                    //add to the selected list
                    if(member.getInterestList() != null){           //check for content in the list
                        member.getInterestList().add(AreaOfInterestDAO.getInstance().read(currentAvail.get(selections[i])));
                    }else{                                         //if the list is null, create it, then build it.
                        member.setInterestList(new ArrayList<AreaOfInterestBO>());
                        member.getInterestList().add(AreaOfInterestDAO.getInstance().read(currentAvail.get(selections[i])));
                    }
                } catch (DataException ex) {
                    ex.printStackTrace();
                } catch (ConnectionPoolException ex) {
                    ex.printStackTrace();
                }
            }
            this.updateSelectedListView();
            this.updateListView();
        }
    }//GEN-LAST:event_button_selectAOIActionPerformed
    
    /** Updates the selected list of Areas of Interest */
    private void updateSelectedListView(){
        ArrayList<String> selection = new ArrayList<String>();
        if(member.getInterestList() != null){
            //build the list of descriptions for display
            for(int i=0; i<member.getInterestList().size(); i++){
                selection.add(member.getInterestList().get(i).getDescription());
            }
            list_selectedAOI.setListData(selection.toArray());
            list_selectedAOI.updateUI();
        }
    }
    
    
    private void field_creditCardNumFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_field_creditCardNumFocusGained
        if(field_creditCardNum.getText().equals("<Credit Card #>")){ field_creditCardNum.setText(""); }
    }//GEN-LAST:event_field_creditCardNumFocusGained
    
    private void button_SaveMembershipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_SaveMembershipActionPerformed
        //if the required fields are not filled in, prompt for correct input
        if(cboBox_CreditCardType.getSelectedItem().toString().equals("<Select>")){ JOptionPane.showMessageDialog(null,"Select a credit card type."); return;}
        
        //check if a credit card number exists
        if(field_creditCardNum.getText().equals("<Credit Card #>")){ JOptionPane.showMessageDialog(null,"Enter a Credit Card Number."); return;}
        //if the credit card number has been changed
        if(field_creditCardNum.getText().equals("****************") == false){
            //check the numerical content of the credit card number
            Matcher m = Pattern.compile("[^\\d\\s.-]").matcher(field_creditCardNum.getText());
            if (m.find()) {
                JOptionPane.showMessageDialog(null,"Credit card number can only contain numbers, spaces, \"-\", and \".\"");
                return;
            }
        }
        
        if(cboBox_Month.getSelectedItem().equals("<select>")){ JOptionPane.showMessageDialog(null,"Select the credit card's\\nexpiration month."); return;}
        if(cboBox_Year.getSelectedItem().equals("<select>")){ JOptionPane.showMessageDialog(null,"Select the credit card's\\nexpiration year."); return;}
        //if(field_memberExpireDate.getText().equals("<mm/dd/yyyy>")){ JOptionPane.showMessageDialog(null,"Enter the expiration date for membership."); }
        
        //when the 'Save' button is clicked
        int opt = JOptionPane.showConfirmDialog(null,"Save Member to the database?","Save?", JOptionPane.YES_NO_OPTION);
        if( opt == JOptionPane.YES_OPTION){
            try{
                if(member == null){
                    member = MembershipDAO.getInstance().create();
                }
                
                //set the values of the object according to UI input if input was entered
                member.setIsDirty(true);
                member.setCreditCardType(cboBox_CreditCardType.getSelectedItem().toString());
                //if the credit card number field has already been hidden, retrieve the data for the output
                if(field_creditCardNum.getText().equals("****************") && CCNum != null){
                    member.setCreditCardNum(CCNum);
                }else{
                    member.setCreditCardNum(field_creditCardNum.getText());
                }
                member.setCreditCardExpMonth(Integer.parseInt(cboBox_Month.getSelectedItem().toString()));
                member.setCreditCardExpYear(Integer.parseInt(cboBox_Year.getSelectedItem().toString()));
                if(this.radio_backupYes.isSelected() == true){
                    if(!this.field_backupSize.getText().equals("") || !this.field_backupSize.getText().equals("<Size>")){
                        member.setBuSize(Double.parseDouble(this.field_backupSize.getText()));
                        member.setBuExpDate(this.field_backupExpDate.getText());
                    }
                }else{
                    String is = null;
                    member.setBuExpDate(is);
                    member.setBuSize(0.0);
                }
                //member.setMemberStartDate(new Date(field_memberStartDate.getText()));  //already set automatically when the object is created
                //member.setMemberExpireDate(new Date(field_memberExpireDate.getText()));  //not sure if this is necessary
                
                //save the Membership object to the DB
                MembershipDAO.getInstance().save(member);
                JOptionPane.showMessageDialog(null,"Member saved to database Successfully.");
                callingFrame.setButton_EditMembState(true);
                callingFrame.setChkBox_YesState(true);
                callingFrame.setChkBox_NoState(false);
                callingFrame.setChkBox_NoEnabled(false);
                callingFrame.setLabel_memberText("Is member?");
                callingFrame.setButton_UseCust(true);
                this.setVisible(false);
                this.dispose();
            }catch (Exception e){
                e.printStackTrace();
            }
            button_SaveMembership.setSelected(false);
            return;
        }else{
            return;
        }
    }//GEN-LAST:event_button_SaveMembershipActionPerformed
    
    private void button_cancelMembershipChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_cancelMembershipChangesActionPerformed
        int opt = JOptionPane.showConfirmDialog(null,"Are you sure you want to cancel?","Cancel Warning", JOptionPane.YES_NO_OPTION);
        if(opt == JOptionPane.YES_OPTION){
            if(member.isAlreadyInDB() == false){
                member.getCust().setCustMembership(null);
                button_cancelMembershipChanges.setSelected(false);
                this.setVisible(false);
                return;
            }else{
                member.setIsDirty(false);                           //changes were cancelled, so the membership doesn't need to be updated
                this.setVisible(false);
                this.dispose();
                return;
            }
        }else{
            button_cancelMembershipChanges.setSelected(false);
            return;
        }
    }//GEN-LAST:event_button_cancelMembershipChangesActionPerformed
    
    private void button_addNewAOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_addNewAOIActionPerformed
        try {                                                                           //change appearance according to system
            String look = UIManager.getSystemLookAndFeelClassName();
            UIManager.setLookAndFeel(look);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        AreaOfInterestFrame AOI_Frame = new AreaOfInterestFrame(this);
        AOI_Frame.setLocationRelativeTo(null);
        AOI_Frame.setVisible(true);                                  //open the AreaOfInterestFrame
    }//GEN-LAST:event_button_addNewAOIActionPerformed
    
    /*now refresh the list according to what was added in the add AOI frame*/
    public synchronized void updateListView(){
        try {
            ArrayList<AreaOfInterestBO> aoiList = AreaOfInterestDAO.getInstance().readList();
            ArrayList<String> availList = new ArrayList<String>();      //set up list for storing descriptions
            currentAvail = new ArrayList<String>();        //reset current available AOI_ID list
            //build the list for the view
            //check the already selected list to provide more realistic selection
            if(member.getInterestList() != null){
                boolean alreadySelected = false;
                for(int i=0; i<aoiList.size(); i++){
                    for(int x=0; x < member.getInterestList().size(); x++){
                        //if the item is not already selected, display it
                        if(aoiList.get(i).getAOI_ID().equals(member.getInterestList().get(x).getAOI_ID())){
                            alreadySelected = true;
                        }
                    }
                    if(alreadySelected == false){
                        availList.add(aoiList.get(i).getDescription());
                        currentAvail.add(aoiList.get(i).getAOI_ID());       //keep a list of the corresponding IDs for later
                    }
                    alreadySelected = false;
                }
            }else{
                for(int z=0; z < aoiList.size(); z++){
                    availList.add(aoiList.get(z).getDescription());
                    currentAvail.add(aoiList.get(z).getAOI_ID());       //keep a list of the corresponding IDs for later
                }
            }
            list_avail_AOI.setListData(availList.toArray());
            list_avail_AOI.updateUI();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new MembershipFrame().setVisible(true);
//            }
//        });
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_SaveMembership;
    private javax.swing.JButton button_addNewAOI;
    private javax.swing.JButton button_cancelMembershipChanges;
    private javax.swing.JButton button_checkBuPrice;
    private javax.swing.JButton button_deselectAOI;
    private javax.swing.JButton button_selectAOI;
    private javax.swing.JComboBox cboBox_CreditCardType;
    private javax.swing.JComboBox cboBox_Month;
    private javax.swing.JComboBox cboBox_Year;
    private javax.swing.JTextField field_backupExpDate;
    private javax.swing.JTextField field_backupSize;
    private javax.swing.JTextField field_creditCardNum;
    private javax.swing.JTextField field_memberExpireDate;
    private javax.swing.JTextField field_memberStartDate;
    private javax.swing.JTextField field_memberStartDate1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel label_AOISelection;
    private javax.swing.JLabel label_AOISelection1;
    private javax.swing.JLabel label_MembershipHead;
    private javax.swing.JLabel label_availAOI;
    private javax.swing.JLabel label_backupExpDate;
    private javax.swing.JLabel label_backupSize;
    private javax.swing.JLabel label_creditCardExpDate;
    private javax.swing.JLabel label_creditCardMonth;
    private javax.swing.JLabel label_creditCardNum;
    private javax.swing.JLabel label_creditCardType;
    private javax.swing.JLabel label_creditCardYear;
    private javax.swing.JLabel label_memberExpireDate;
    private javax.swing.JLabel label_memberStartDate;
    private javax.swing.JLabel label_selectedAOI;
    private javax.swing.JList list_avail_AOI;
    private javax.swing.JList list_selectedAOI;
    private javax.swing.JRadioButton radio_backupNo;
    private javax.swing.JRadioButton radio_backupYes;
    // End of variables declaration//GEN-END:variables
    
}
